/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManageBean;

import UserBN.Authentication;
import UserBN.UserService;
import entity.House;
import entity.LinkImage;
import entity.SwhUser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author Mr-Quick
 */
@ManagedBean
@SessionScoped
public class LeaseMB {

    public LeaseMB() {
        photos = new ArrayList<LinkImage>();
        
    }
    String title;
    int bedroom;
    int bathroom;
    int livingroom;
    String SquarefootageEarth;
    String SquarefootageHouse;
    String city;
    String location;
    String typehouse;
    String type = "Lease";
    Double deposit;
    String introduction;
    String payment;
    java.util.Date date = new Date();
    String linkmap;

    public String getLinkmap() {
        return linkmap;
    }

    public void setLinkmap(String linkmap) {
        this.linkmap = linkmap;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSquarefootageEarth() {
        return SquarefootageEarth;
    }

    public void setSquarefootageEarth(String SquarefootageEarth) {
        this.SquarefootageEarth = SquarefootageEarth;
    }

    public String getSquarefootageHouse() {
        return SquarefootageHouse;
    }

    public void setSquarefootageHouse(String SquarefootageHouse) {
        this.SquarefootageHouse = SquarefootageHouse;
    }

    public int getBathroom() {
        return bathroom;
    }

    public void setBathroom(int bathroom) {
        this.bathroom = bathroom;
    }

    public int getBedroom() {
        return bedroom;
    }

    public void setBedroom(int bedroom) {
        this.bedroom = bedroom;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Double getDeposit() {
        return deposit;
    }

    public void setDeposit(Double deposit) {
        this.deposit = deposit;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public int getLivingroom() {
        return livingroom;
    }

    public void setLivingroom(int livingroom) {
        this.livingroom = livingroom;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getPayment() {
        return payment;
    }

    public void setPayment(String payment) {
        this.payment = payment;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTypehouse() {
        return typehouse;
    }

    public void setTypehouse(String typehouse) {
        this.typehouse = typehouse;
    }
    List<LinkImage> photos;

    public List<LinkImage> getPhotos() {
        return photos;
    }

    public String Addlease()
    {
        UserService us = new UserService();
        Authentication auth = Authentication.getInstance();
        SwhUser u = us.findAccountByUsername(auth.getUsername());
        if (u != null) {
            House h = new House();
            h.setBathroom(bathroom);
            h.setBedroom(bedroom);
            h.setCity(city);
            h.setDateUp(date);
            h.setDeposit(deposit);
            h.setIdHouse(UUID.randomUUID().toString());
            h.setIntroduction(introduction);
            h.setLivingroom(livingroom);
            h.setLocation(location);
            h.setSquarefootageEarth(SquarefootageEarth);
            h.setSquarefootageHouse(SquarefootageHouse);
            h.setTittlehouse(typehouse);
            h.setHomestyle(type);
            h.setTypehouse(typehouse);
            h.setTypepayment(payment);
            h.setTittlehouse(title);
            h.setLinkmap(linkmap);
            h.setIDUser(u);
            
            try {
                HouseBN.SellerBN hs = new HouseBN.SellerBN();
                hs.Addseller(h, u);
                LinkImageBN.InsertimginDB LIBN = new LinkImageBN.InsertimginDB();
                LIBN.insertimg(photos, h.getIdHouse());
                return "/DetailsMessage.xhtml?faces-redirect=true&id=" + h.getIdHouse() ;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }
    
      public void handleFileUpload(FileUploadEvent event) {
        FileOutputStream outStream = null;
        try {
            ExternalContext exContext = FacesContext.getCurrentInstance().getExternalContext();
            String fileName = event.getFile().getFileName();
            String ext = fileName.substring(fileName.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString() + ext;
            String realFolder = exContext.getRealPath("/images/");
            File file = new File(realFolder);
            if (!file.exists()) {
                file.mkdirs();
            }
            outStream = new FileOutputStream(realFolder + "/" + newFileName);
            outStream.write(event.getFile().getContents());
            entity.LinkImage photo = new entity.LinkImage();
            photo.setId(0);
            photo.setLinkImageHouse("/images/" + newFileName);
            photos.add(photo);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(UploadPicture.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(UploadPicture.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                outStream.close();
            } catch (IOException ex) {
                Logger.getLogger(UploadPicture.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
